package com.hua.common.commonview;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Cap;
import android.graphics.Paint.FontMetricsInt;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.animation.OvershootInterpolator;
import android.widget.LinearLayout;

import com.hua.common.commontools.DipUtil;

/**
 * 码表控件
 *
 * @author huaxiaolin_pc
 */

public class ArcControl extends LinearLayout {

	/**
	 * 控件自身的宽高
	 */
	private int mWidth = 0;
	/**
	 * 状态值
	 */
	private int stat = 0;
	/**
	 * 圆环背景色和前景色 以及文字的颜色
	 */
	private int roundBackColor, roundForegroundColor, textColor;
	/**
	 * 颜色列表
	 */
	private int colors[];
	/**
	 * 停留的百分数
	 */
	private float Percentage = 0f;
	/**
	 * 角度转换弧度公式
	 */
	public double PI = 2 * Math.PI / 360;
	/**
	 * 圆环的背景画笔，圆环的前景画笔，文字画笔
	 */
	private Paint backPaint, foreGroundPaint, textPaint = null;
	/**
	 * 圆环总度数
	 */
	private float cureRoud = 240;

	private MyHandler handler = null;
	/**
	 * 值动画
	 */
	private ValueAnimator anim = null;

	public RoundChangeListener changeListener = null;

	/**
	 * 圆环画笔的宽度
	 */
	private float paintWidth = 0;
	/**
	 * 内边距宽度
	 */
	private float padingWidth = 0;
	/**
	 * 绘制文字的画笔的宽度
	 */
	private float textPaintWidth = 0f;
	/**
	 * 文字的大小
	 */
	private float textSize = 0f;

	public ArcControl(Context context) {
		super(context);
		this.init();
	}

	public ArcControl(Context context, AttributeSet attr) {
		super(context, attr);
		this.init();
	}

	public ArcControl(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		this.init();
	}

	/**
	 * 初始化
	 */
	private void init() {
		this.handler = new MyHandler();
		this.initColors();
		this.initPaint();
	}

	/**
	 * 获取控件自身的宽度
	 */
	private void initWH() {
		mWidth = getMeasuredWidth();
	}

	/**
	 * 初始化颜色
	 */
	private void initColors() {
		roundBackColor = Color.parseColor("#ffc0c0c0");
		roundForegroundColor = Color.parseColor("#0085cf");
		textColor = Color.parseColor("#ff888888");
		this.colors = new int[] { roundForegroundColor, roundForegroundColor,
				roundForegroundColor, roundForegroundColor };
	}

	/**
	 * 初始化画笔
	 */
	private void initPaint() {
		this.paintWidth = DipUtil.Dp2px(getContext(), 10);
		this.padingWidth = DipUtil.Dp2px(getContext(), 8);
		this.textPaintWidth = DipUtil.Dp2px(getContext(), 1f);
		this.textSize = DipUtil.Sp2px(getContext(), 10);

		backPaint = new Paint();
		backPaint.setAntiAlias(true);
		backPaint.setStyle(Style.STROKE);
		backPaint.setStrokeWidth(this.paintWidth);
		backPaint.setAntiAlias(true); // 消除锯齿
		backPaint.setDither(true);
		backPaint.setStrokeCap(Cap.ROUND); // 当画笔样式为STROKE或FILL_OR_STROKE时，设置笔刷的图形样式，如圆形样式
		// Cap.ROUND,或方形样式Cap.SQUARE
		backPaint.setColor(roundBackColor);

		textPaint = new Paint();
		textPaint.setAntiAlias(true);
		textPaint.setStyle(Style.FILL);
		textPaint.setStrokeWidth(this.textPaintWidth);
		textPaint.setTextSize(this.textSize);
		textPaint.setAntiAlias(true); // 消除锯齿
		textPaint.setDither(true);
		textPaint.setStrokeCap(Cap.ROUND); // 当画笔样式为STROKE或FILL_OR_STROKE时，设置笔刷的图形样式，如圆形样式
		// Cap.ROUND,或方形样式Cap.SQUARE
		textPaint.setColor(textColor);

		foreGroundPaint = new Paint();
		foreGroundPaint.setAntiAlias(true);
		foreGroundPaint.setStyle(Style.STROKE);
		foreGroundPaint.setStrokeWidth(this.paintWidth);
		foreGroundPaint.setAntiAlias(true); // 消除锯齿
		foreGroundPaint.setDither(true);
		foreGroundPaint.setStrokeCap(Cap.ROUND); // 当画笔样式为STROKE或FILL_OR_STROKE时，设置笔刷的图形样式，如圆形样式
		// Cap.ROUND,或方形样式Cap.SQUARE
		foreGroundPaint.setColor(roundForegroundColor);
	}

	private void initAnim() {
		anim = ValueAnimator.ofFloat(240, this.Percentage);
		anim.setInterpolator(new OvershootInterpolator());
		anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
			@Override
			public void onAnimationUpdate(ValueAnimator animation) {
				float myRound=(float) animation.getAnimatedValue();
				if (myRound> 0f) {
					cureRoud = myRound;
					changeArcColor(myRound);
				}
				System.out.println("" + cureRoud);
			}
		});
		anim.setDuration(8000);
	}

	/**
	 * 启动定时器，进行自动绘制前景圆环
	 */
	public void start() {
		cancle();
		stat = 1;
		if (anim == null) {
			this.initAnim();
		}
		anim.start();
	}

	/**
	 * 停止计时器，并且停止绘制
	 */
	public void cancle() {
		if (anim != null) {
			this.anim.cancel();
		}
	}

	public void reset() {
		cancle();
		stat = 0;
		this.handler.sendEmptyMessageDelayed(1, 100);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		this.initWH();
	}

	/**
	 * 设置颜色列表
	 *
	 */
	public void setColorList(int colors[]) {
		if (colors.length < 4) {
		} else {
			this.colors[0] = colors[0];
			this.colors[1] = colors[1];
			this.colors[2] = colors[2];
			this.colors[3] = colors[3];
		}
	}

	/**
	 * 设置绘制前景圆环时的回调接口 前景圆环绘制一个刻度 此接口回调一次
	 *
	 * @param changeListener
	 */
	public void setChangerListener(RoundChangeListener changeListener) {
		this.changeListener = changeListener;
	}

	@Override
	public void draw(Canvas canvas) {
		super.draw(canvas);
		drawAcr(canvas);
		drawText(canvas);
		if (stat == 1) {
			drawOutArc(canvas);
		}
	}

	/**
	 * 绘制背景圆环
	 *
	 * @param canvas
	 */
	private void drawAcr(Canvas canvas) {
		RectF oval1 = new RectF(this.padingWidth, this.padingWidth, mWidth
				- this.padingWidth, mWidth - this.padingWidth);
		canvas.drawArc(oval1, 150, 240, false, backPaint);// 小弧形
	}

	/**
	 * 设置停留的百分比
	 *
	 * @param Percentage
	 */
	public void setPercentage(float Percentage) {
		this.Percentage = (float) (Percentage * (2.4));
	}

	/**
	 * 给圆环绘制刻度
	 *
	 * @param canvas
	 */
	private void drawText(Canvas canvas) {
		int roundCenter = mWidth / 2; // 圆心的横轴坐标
		int Radius = (int) (mWidth / 2 - this.padingWidth - this.paintWidth - this.textSize);

		double x0 = roundCenter - Radius * Math.cos(30 * PI);
		double y0 = roundCenter + Radius * Math.sin(30 * PI);
		drawTextPosition(canvas, (int) x0, (int) y0, textPaint, "0%");

		double x1 = roundCenter - Radius * Math.cos(6 * PI);
		double y1 = roundCenter + Radius * Math.sin(6 * PI);
		drawTextPosition(canvas, (int) x1, (int) y1, textPaint, "10%");

		double x2 = roundCenter - Radius * Math.cos(18 * PI);
		double y2 = roundCenter - Radius * Math.sin(18 * PI);
		drawTextPosition(canvas, (int) x2, (int) y2, textPaint, "20%");

		double x3 = roundCenter;
		double y3 = this.padingWidth + this.paintWidth;
		drawTextPosition(canvas, (int) x3, (int) y3, textPaint, "50%");

		double x4 = roundCenter + Radius * Math.cos(30 * PI);
		double y4 = roundCenter + Radius * Math.sin(30 * PI);
		drawTextPosition(canvas, (int) x4, (int) y4, textPaint, "100%");
	}

	/**
	 * 绘制文字的时候左右居中
	 *
	 * @param canvas
	 * @param x
	 * @param y
	 * @param paint
	 * @param text
	 */
	private void drawTextPosition(Canvas canvas, int x, int y, Paint paint,
								  String text) {
		Rect targetRect = new Rect(x - 10, y - 10, x + 10, y + 10);
		paint.setColor(Color.parseColor("#00ffffff"));
		canvas.drawRect(targetRect, paint);
		paint.setColor(textColor);
		FontMetricsInt fontMetrics = paint.getFontMetricsInt();
		int baseline = targetRect.top
				+ (targetRect.bottom - targetRect.top - fontMetrics.bottom + fontMetrics.top)
				/ 2 - fontMetrics.top;
		// 下面这行是实现水平居中，drawText对应改为传入targetRect.centerX()
		paint.setTextAlign(Paint.Align.CENTER);
		canvas.drawText(text, targetRect.centerX(), baseline, paint);
	}

	/**
	 * 绘制圆环的前景色
	 *
	 * @param canvas
	 */
	private void drawOutArc(Canvas canvas) {
		RectF oval1 = new RectF(this.padingWidth, this.padingWidth, mWidth
				- this.padingWidth, mWidth - this.padingWidth);
		canvas.drawArc(oval1, 150, cureRoud, false, foreGroundPaint);// 小弧形
	}

	class MyHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
				case 1:
					invalidate();
					break;
			}
		}
	}

	/**
	 * 改变画笔夜色的方法
	 *
	 * @param roud
	 */
	private void changeArcColor(float roud) {
		if (changeListener != null) {
			changeListener.onchanged(roud/240);
		}
		if (roud >= 120) { // 表示百分之51到百分之100
			foreGroundPaint.setColor(colors[3]);
		} else if (roud >= 48) { // 表示百分之21到百分之50
			foreGroundPaint.setColor(colors[2]);
		} else if (roud >= 24) { // 表示百分之11到百分之20
			foreGroundPaint.setColor(colors[1]);
		} else if (roud >= 0) { // 表示百分之0到百分之10
			foreGroundPaint.setColor(colors[0]);
		} else {
		}
		invalidate();
	}

	public interface RoundChangeListener {
		void onchanged(float ratio);
	}

}
